package cn.smbms.service.provider;

import java.util.List;

import cn.smbms.dao.bill.BillMapper;
import cn.smbms.dao.provider.ProviderMapper;
import cn.smbms.pojo.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class ProviderServiceImpl implements ProviderService {
	
	@Autowired
	private ProviderMapper providerMapper;
	@Autowired
	private BillMapper billDao;
	
	@Override
	public boolean add(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
			if(providerMapper.add(provider) > 0) flag = true;
		return flag;
	}

	@Override
	public List<Provider> getProviderList(String proName,String proCode, Integer currentPageNo,int pageSize) {
		return providerMapper.getProviderList(proName,proCode,(currentPageNo-1)*pageSize,pageSize);
	}

	/**
	 * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作
	 * 若订单表中无该供应商的订单数据，则可以删除
	 * 若有该供应商的订单数据，则不可以删除
	 * 返回值billCount
	 * 1> billCount == 0  删除---1 成功 （0） 2 不成功 （-1）
	 * 2> billCount > 0    不能删除 查询成功（0）查询不成功（-1）
	 * 
	 * ---判断
	 * 如果billCount = -1 失败
	 * 若billCount >= 0 成功
	 */
	@Override
	public int deleteProviderById(String delId) {
		int billCount = -1;
		try {
			billCount = billDao.getBillCountByProviderId(delId);
			if(billCount == 0){
				providerMapper.deleteProviderById(delId);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			billCount = -1;

		}
		return billCount;
	}

	@Override
	public Provider getProviderById(String id) {

		return providerMapper.getProviderById(id);
	}

	@Override
	public boolean modify(Provider provider) {
		boolean flag = false;
		try {
			if(providerMapper.modify(provider) > 0)
				flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int getcount(String queryProName, String queryProCode) {
		return providerMapper.getcount(queryProName,queryProCode);
	}

}
